package com.ddwl.shop.controller;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.exception.ParamException;
import com.ddwl.schema.bo.shop.ShopRegBo;
import com.ddwl.schema.bo.user.RegBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.order.ShopOrderDataVo;
import com.ddwl.schema.vo.shop.ShopVo;
import com.ddwl.shop.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 类ShopConsoleController.java的实现描述：TODO
 *
 * @author zach
 * @Date :2020-12-04 20:52.
 */
@Api(tags = "门店商户后台接口")
@RestController
public class ShopConsoleController extends BaseController {

    @Resource
    private ShopService shopService;

    @GetMapping("/loginInfo")
    @ApiOperation("获取当前登录门店信息")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo<ShopVo> loginShop() {
        return RestVo.ok( shopService.getShop(getShopId()));
    }


    @PostMapping("/shop/pwd/change")
    @ApiOperation("修改密码")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo changeShopPwd(@Valid @RequestBody ShopRegBo bo) {
        shopService.changeShopPwd(getShopId(),bo);
        return RestVo.ok();
    }

    @Deprecated
    @PostMapping("/shop/pwd/reset")
    @ApiOperation("重置密码")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<ShopVo> changeShopPwd(@Valid @RequestBody RegBo bo) {
        bo.setProduct(getAuthMap().get("pk"));
        shopService.resetShopPwd(bo);
        return RestVo.ok();
    }

    @PostMapping("/shopConsole/change/logistics")
    @ApiOperation("开启物流")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo changeShopLogistics(@RequestParam Integer logisticsTag) {
        shopService.changeShopLogistics(getShopId(),logisticsTag,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/shopConsole/change/localDelivery")
    @ApiOperation("开启本地配送")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo changeShopLocalDelivery(@RequestParam Integer localDeliveryTag) {
        shopService.changeShopLocalDeliveryTag(getShopId(),localDeliveryTag,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/shopConsole/change/selfMention")
    @ApiOperation("开启自提")
    @Auth(product = BaseEnum.ProductEnum.SHOP_CONSOLE)
    public RestVo changeShopSelfMention(@RequestParam Integer selfMentionTag) {
        shopService.changeShopSelfMention(getShopId(),selfMentionTag,getUserId());
        return RestVo.ok();
    }

    @PostMapping("/shopConsole/getData")
    @ApiOperation("查询统计数据")
    @Auth(product = {BaseEnum.ProductEnum.SHOP_CONSOLE,BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ShopOrderDataVo> getData() {
        ShopOrderDataVo vo=shopService.getData(getShopId());
        return RestVo.ok(vo);
    }

    @PostMapping("/shopConsole/getWbmData")
    @ApiOperation("查询wbm后台统计数据")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ShopOrderDataVo> getWbmData() {
        String shopId= shopService.getShopIdByUserId(getUserId());
        ParamException.isBlank(shopId,"当前用户门店不存在");
        ShopOrderDataVo vo=shopService.getData(shopId);
        return RestVo.ok(vo);
    }

    @PostMapping("/shopConsole/getWbmDataOpenId")
    @ApiOperation("员工查询wbm后台统计数据")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ShopOrderDataVo> getWbmData(@RequestParam String shopId) {
        getShopVo(shopId);
        ShopOrderDataVo vo=shopService.getData(shopId);
        return RestVo.ok(vo);
    }

    @GetMapping("/getShopWxaCode")
    @ApiOperation("获取店铺小程序码（放大处理过）")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE,BaseEnum.ProductEnum.SHOP_CONSOLE})
    public RestVo<String> getShopWxaCode(@RequestParam String shopId){
        if(StrUtil.isEmpty(shopId)){
            shopId = getShopId();
        }
        return RestVo.ok(shopService.getShopWxaCode(shopId));
    }

}
